home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 03 - Advanced Graphics / Example 1 / main.c < prev    next >
C/C++ Source or Header  |  1995-03-05  |  5KB  |  241 lines

  1. //
  2. //    File: main.c
  3. //
  4. //    This file contains the code for a VERY minimal application.
  5. //    It will bring up a set an app palette, open a window and
  6. //     run a sprite demo. 
  7. //
  8. //    2/18/95 -- Created by Mick
  9. //
  10.  
  11. // include files
  12.  
  13. #include <profiler.h>
  14.  
  15. #include "global.h"
  16.  
  17. #include <LowMem.h>        // the CodeWarrior version -- if you are using Symantec, include LoMem.h instead
  18. #include <Palettes.h>
  19.  
  20. #include "main.h"
  21.  
  22. #include "demo.h"
  23.  
  24. // defines for this file
  25.  
  26. // global function declarations
  27.  
  28. void main( void );
  29.  
  30. // global data owned by this file
  31.  
  32. WindowPtr gMainWindow;                    // the window that we are drawing to
  33. CTabHandle gAppColorTable;            // the color table that we are drawing with
  34.  
  35. // local function declarations
  36.  
  37. static void initMacToolboxes( void );
  38. static void doEventLoop( void );
  39. static void hideMenuBar( void );
  40. static void showMenuBar( void );
  41.  
  42. // static data
  43.  
  44. static unsigned short sMenuBarHeight;            // the height of the menu bar when the program started
  45. static RgnHandle sOriginalGrayRgn;                                // the gray region when the program started
  46.  
  47. // functions
  48.  
  49. //
  50. // main -
  51. //
  52. //        This is where it all begins.
  53. //
  54.  
  55. void main( void )
  56. {
  57.     PaletteHandle appPalette;            // the palette that we will make the application palette
  58.     
  59.     // startup the profiler
  60. #if __profile__
  61.     if ( ProfilerInit( collectDetailed, bestTimeBase, 100, 10 ) != noErr )
  62.         {
  63.             Debugger();
  64.         }
  65. #endif
  66.  
  67.     // fire up the toolboxes
  68.     initMacToolboxes();
  69.     
  70.     // get the color table and make it the default palette
  71.     gAppColorTable = GetCTable( kAppColorTableResID );
  72.     appPalette = NewPalette( 256, gAppColorTable, pmExplicit + pmTolerant, 0 );
  73.     SetPalette( ( WindowPtr )-1L,    appPalette,    kFalse    );
  74.     
  75.     // hide the menu bar
  76.     hideMenuBar();
  77.     
  78.     // create and show the window
  79.     gMainWindow = GetNewCWindow( kMainWindowResID, ( Ptr )kNil, kWindowInFront );
  80.     ShowWindow( gMainWindow );
  81.     SetPort( gMainWindow );
  82.  
  83.     // give the demo code a chance to setup
  84.     startupDemo();
  85.     
  86.     // handle events until the user signals a quit
  87.     doEventLoop();
  88.     
  89.     // give the demo code a chance to shutdown
  90.     shutdownDemo();
  91.     
  92.     // hide and delete the window
  93.     HideWindow( gMainWindow );
  94.     DisposeWindow( gMainWindow );
  95.     
  96.     // show the menu bar again
  97.     showMenuBar();
  98.  
  99.     // shutdown the profiler
  100. #if __profile__
  101.     ProfilerDump( "\pDemo.prof" );
  102.     ProfilerTerm();
  103. #endif
  104. }
  105.  
  106. //
  107. // initMacToolboxes -
  108. //
  109. //        Initialize all the toolboxes that we will need.
  110. //
  111.  
  112. void initMacToolboxes( void )
  113. {
  114.     // initialize all the toolboxes
  115.     InitGraf( &( qd.thePort ) );
  116.     InitFonts();
  117.     InitWindows();
  118.     InitMenus();
  119.     TEInit();
  120.     InitDialogs( ( ProcPtr )kNil );
  121.     InitCursor();
  122.     
  123.     // make sure that we have the enitre heap
  124.     MaxApplZone();
  125.     
  126.     // allocate some extra master pointers
  127.     MoreMasters();
  128.     MoreMasters();
  129.     MoreMasters();
  130.     MoreMasters();
  131.     
  132.     // set the random seed
  133.     qd.randSeed = TickCount();
  134. }
  135.  
  136.  
  137. //
  138. //    doEventLoop -
  139. //
  140. //    Get and process events until the user signals a quit.
  141. //
  142.  
  143. void doEventLoop( void )
  144. {
  145.     unsigned char quitNow;            // a flag that notes if the user has signaled a quit
  146.     EventRecord theEvent;                    // a place to put an event
  147.     
  148.     quitNow = kFalse;
  149.     while( !quitNow )
  150.         {
  151.             if( WaitNextEvent( everyEvent, &theEvent, 0L, ( RgnHandle )kNil ) == kTrue )
  152.                 {
  153.                     switch( theEvent.what )
  154.                         {
  155.                             case mouseDown:
  156.                                 // give the demo uninteruppted time to draw
  157.                                 while( StillDown() )
  158.                                     {
  159.                                         doDemoFrame();
  160.                                     }
  161.                                 break;
  162.                             case keyDown:
  163.                                 // check to see if the user has hit cmd-q (to signal a quit)
  164.                                 if ( ( theEvent.modifiers & cmdKey ) && ( ( theEvent.message & charCodeMask ) == 'q' ) )
  165.                                     {
  166.                                         quitNow = kTrue;
  167.                                     }
  168.                                 else
  169.                                     {
  170.                                         // send the key to the demo code
  171.                                         demoKey( ( unsigned char )( theEvent.message & charCodeMask ) );
  172.                                     }
  173.                                 break;
  174.                             case updateEvt:
  175.                                 BeginUpdate( gMainWindow );
  176.                                 EndUpdate( gMainWindow );
  177.                             default:
  178.                                 break;
  179.                         }
  180.                 }
  181.             else
  182.                 {
  183.                     // give the demo code some time to run
  184.                     doDemoFrame();
  185.                 }
  186.         }
  187. }
  188.  
  189.  
  190. //
  191. //    hideMenuBar -
  192. //
  193. //    Hides the menu bar and adds it to the area that can be drawn
  194. //
  195.  
  196. void hideMenuBar( void )
  197. {
  198.     GDHandle mainScreen;                    // the information on the main screen
  199.     Rect mainScreenRect;                            // the rect that bounds the menu bar
  200.     RgnHandle mainScreenRgn;                // the region of the menu bar
  201.  
  202.     // record the menu bar height
  203.     sMenuBarHeight = GetMBarHeight();
  204.  
  205.     // set the menu bar to no height
  206.     LMSetMBarHeight( 0 );
  207.     
  208.     // save the original gray regions (so we can restore it later)
  209.     sOriginalGrayRgn = NewRgn();
  210.     CopyRgn( GetGrayRgn(), sOriginalGrayRgn );
  211.  
  212.     // make sure that the entire main screen is ours to play with (no menu bars or corners)
  213.     mainScreenRgn = NewRgn();
  214.     mainScreen = GetMainDevice();
  215.     mainScreenRect = ( ( *mainScreen )->gdRect );
  216.     RectRgn( mainScreenRgn, &mainScreenRect );
  217.     UnionRgn( sOriginalGrayRgn, mainScreenRgn, GetGrayRgn() );
  218.     
  219.     // draw the desktop
  220.     PaintOne( ( WindowRef )kNil, sOriginalGrayRgn );
  221. }
  222.  
  223.  
  224. //
  225. //    showMenuBar -
  226. //
  227. //    Shows the menu bar and removes it from the drawable area.
  228. //
  229.  
  230. void showMenuBar( void )
  231. {
  232.     // set the menu bar to its normal height
  233.     LMSetMBarHeight( sMenuBarHeight );
  234.     
  235.     // restore the original gray region
  236.     SectRgn( sOriginalGrayRgn, GetGrayRgn(), GetGrayRgn() );
  237.     
  238.     // draw the menu bar
  239.     DrawMenuBar();
  240. }
  241.